home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1994 #2
/
Monster Media No. 2 (Monster Media)(1994).ISO
/
prog_c
/
tpchal_1.zip
/
RG.C0
< prev
next >
Wrap
Text File
|
1994-05-26
|
1KB
|
37 lines
/* solve.c -- find solution to simple programming challenge
**
** public domain by Ray Gardner 5/94
**
** Find a 9-digit number, using all digits 1 thru 9 (once each),
** such that the first n digits are divisible by n, for n <= 9.
*/
#include <stdio.h>
void try(long n, int k, char *digits) /* recursive solver */
{
int i;
for ( i = 1; i < 10; ++i ) /* look thru remaining digits */
{
if ( digits[i] != '*' ) { /* if not already used */
long nn = n * 10 + i; /* next candidate */
if ( nn % k == 0 ) { /* passes divisibility test? */
if ( k == 9 ) { /* if 9 digits, print solution */
printf("%ld\n", nn);
} else { /* else extend partial solution */
digits[i] = '*'; /* flag digit as used */
try(nn, k+1, digits); /* solve */
digits[i] = i + '0'; /* unflag digit */
}
}
}
}
}
int main(void)
{
char digits[] = "0123456789";
try(0L, 1, digits); /* initial state */
return 0;
}